#include <cstdio>
#include <windows.h>
#include "CMImage.h"

#define CLIPPING(_x)        ((_x<0) ? 0 : ((_x>255) ? 255 : _x))

float contbl_Y[256] = { 
    -18.624f, -17.46f, -16.296f, -15.132f, -13.968f, -12.804f, -11.64f, -10.476f, -9.312f, -8.148f, -6.984f, -5.82f, -4.656f, -3.492f, -2.328f, -1.164f, 
    0.f, 1.164f, 2.328f, 3.492f, 4.656f, 5.82f, 6.984f, 8.148f, 9.312f, 10.476f, 11.64f, 12.804f, 13.968f, 15.132f, 16.296f, 17.46f, 
    18.624f, 19.788f, 20.952f, 22.116f, 23.28f, 24.444f, 25.608f, 26.772f, 27.936f, 29.1f, 30.264f, 31.428f, 32.592f, 33.756f, 34.92f, 36.084f, 
    37.248f, 38.412f, 39.576f, 40.74f, 41.904f, 43.068f, 44.232f, 45.396f, 46.56f, 47.724f, 48.888f, 50.052f, 51.216f, 52.38f, 53.544f, 54.708f, 
    55.872f, 57.036f, 58.2f, 59.364f, 60.528f, 61.692f, 62.856f, 64.02f, 65.184f, 66.348f, 67.512f, 68.676f, 69.84f, 71.004f, 72.168f, 73.332f, 
    74.496f, 75.66f, 76.824f, 77.988f, 79.152f, 80.316f, 81.48f, 82.644f, 83.808f, 84.972f, 86.136f, 87.3f, 88.464f, 89.628f, 90.792f, 91.956f, 
    93.12f, 94.284f, 95.448f, 96.612f, 97.776f, 98.94f, 100.104f, 101.268f, 102.432f, 103.596f, 104.76f, 105.924f, 107.088f, 108.252f, 109.416f, 110.58f, 
    111.744f, 112.908f, 114.072f, 115.236f, 116.4f, 117.564f, 118.728f, 119.892f, 121.056f, 122.22f, 123.384f, 124.548f, 125.712f, 126.876f, 128.04f, 129.204f, 
    130.368f, 131.532f, 132.696f, 133.86f, 135.024f, 136.188f, 137.352f, 138.516f, 139.68f, 140.844f, 142.008f, 143.172f, 144.336f, 145.5f, 146.664f, 147.828f, 
    148.992f, 150.156f, 151.32f, 152.484f, 153.648f, 154.812f, 155.976f, 157.14f, 158.304f, 159.468f, 160.632f, 161.796f, 162.96f, 164.124f, 165.288f, 166.452f, 
    167.616f, 168.78f, 169.944f, 171.108f, 172.272f, 173.436f, 174.6f, 175.764f, 176.928f, 178.092f, 179.256f, 180.42f, 181.584f, 182.748f, 183.912f, 185.076f, 
    186.24f, 187.404f, 188.568f, 189.732f, 190.896f, 192.06f, 193.224f, 194.388f, 195.552f, 196.716f, 197.88f, 199.044f, 200.208f, 201.372f, 202.536f, 203.7f, 
    204.864f, 206.028f, 207.192f, 208.356f, 209.52f, 210.684f, 211.848f, 213.012f, 214.176f, 215.34f, 216.504f, 217.668f, 218.832f, 219.996f, 221.16f, 222.324f, 
    223.488f, 224.652f, 225.816f, 226.98f, 228.144f, 229.308f, 230.472f, 231.636f, 232.8f, 233.964f, 235.128f, 236.292f, 237.456f, 238.62f, 239.784f, 240.948f, 
    242.112f, 243.276f, 244.44f, 245.604f, 246.768f, 247.932f, 249.096f, 250.26f, 251.424f, 252.588f, 253.752f, 254.916f, 256.08f, 257.244f, 258.408f, 259.572f, 
    260.736f, 261.9f, 263.064f, 264.228f, 265.392f, 266.556f, 267.72f, 268.884f, 270.048f, 271.212f, 272.376f, 273.54f, 274.704f, 275.868f, 277.032f, 278.196f, 
};
static float contbl_Cb_B[256] = { 
    -258.304f, -256.286f, -254.268f, -252.25f, -250.232f, -248.214f, -246.196f, -244.178f, -242.16f, -240.142f, -238.124f, -236.106f, -234.088f, -232.07f, -230.052f, -228.034f, 
    -226.016f, -223.998f, -221.98f, -219.962f, -217.944f, -215.926f, -213.908f, -211.89f, -209.872f, -207.854f, -205.836f, -203.818f, -201.8f, -199.782f, -197.764f, -195.746f, 
    -193.728f, -191.71f, -189.692f, -187.674f, -185.656f, -183.638f, -181.62f, -179.602f, -177.584f, -175.566f, -173.548f, -171.53f, -169.512f, -167.494f, -165.476f, -163.458f, 
    -161.44f, -159.422f, -157.404f, -155.386f, -153.368f, -151.35f, -149.332f, -147.314f, -145.296f, -143.278f, -141.26f, -139.242f, -137.224f, -135.206f, -133.188f, -131.17f, 
    -129.152f, -127.134f, -125.116f, -123.098f, -121.08f, -119.062f, -117.044f, -115.026f, -113.008f, -110.99f, -108.972f, -106.954f, -104.936f, -102.918f, -100.9f, -98.882f, 
    -96.864f, -94.846f, -92.828f, -90.81f, -88.792f, -86.774f, -84.756f, -82.738f, -80.72f, -78.702f, -76.684f, -74.666f, -72.648f, -70.63f, -68.612f, -66.594f, 
    -64.576f, -62.558f, -60.54f, -58.522f, -56.504f, -54.486f, -52.468f, -50.45f, -48.432f, -46.414f, -44.396f, -42.378f, -40.36f, -38.342f, -36.324f, -34.306f, 
    -32.288f, -30.27f, -28.252f, -26.234f, -24.216f, -22.198f, -20.18f, -18.162f, -16.144f, -14.126f, -12.108f, -10.09f, -8.072f, -6.054f, -4.036f, -2.018f, 
    0.f, 2.018f, 4.036f, 6.054f, 8.072f, 10.09f, 12.108f, 14.126f, 16.144f, 18.162f, 20.18f, 22.198f, 24.216f, 26.234f, 28.252f, 30.27f, 
    32.288f, 34.306f, 36.324f, 38.342f, 40.36f, 42.378f, 44.396f, 46.414f, 48.432f, 50.45f, 52.468f, 54.486f, 56.504f, 58.522f, 60.54f, 62.558f, 
    64.576f, 66.594f, 68.612f, 70.63f, 72.648f, 74.666f, 76.684f, 78.702f, 80.72f, 82.738f, 84.756f, 86.774f, 88.792f, 90.81f, 92.828f, 94.846f, 
    96.864f, 98.882f, 100.9f, 102.918f, 104.936f, 106.954f, 108.972f, 110.99f, 113.008f, 115.026f, 117.044f, 119.062f, 121.08f, 123.098f, 125.116f, 127.134f, 
    129.152f, 131.17f, 133.188f, 135.206f, 137.224f, 139.242f, 141.26f, 143.278f, 145.296f, 147.314f, 149.332f, 151.35f, 153.368f, 155.386f, 157.404f, 159.422f, 
    161.44f, 163.458f, 165.476f, 167.494f, 169.512f, 171.53f, 173.548f, 175.566f, 177.584f, 179.602f, 181.62f, 183.638f, 185.656f, 187.674f, 189.692f, 191.71f, 
    193.728f, 195.746f, 197.764f, 199.782f, 201.8f, 203.818f, 205.836f, 207.854f, 209.872f, 211.89f, 213.908f, 215.926f, 217.944f, 219.962f, 221.98f, 223.998f, 
    226.016f, 228.034f, 230.052f, 232.07f, 234.088f, 236.106f, 238.124f, 240.142f, 242.16f, 244.178f, 246.196f, 248.214f, 250.232f, 252.25f, 254.268f, 256.286f, 
};
static float contbl_Cb_G[256] = { 
    -50.048f, -49.657f, -49.266f, -48.875f, -48.484f, -48.093f, -47.702f, -47.311f, -46.92f, -46.529f, -46.138f, -45.747f, -45.356f, -44.965f, -44.574f, -44.183f, 
    -43.792f, -43.401f, -43.01f, -42.619f, -42.228f, -41.837f, -41.446f, -41.055f, -40.664f, -40.273f, -39.882f, -39.491f, -39.1f, -38.709f, -38.318f, -37.927f, 
    -37.536f, -37.145f, -36.754f, -36.363f, -35.972f, -35.581f, -35.19f, -34.799f, -34.408f, -34.017f, -33.626f, -33.235f, -32.844f, -32.453f, -32.062f, -31.671f, 
    -31.28f, -30.889f, -30.498f, -30.107f, -29.716f, -29.325f, -28.934f, -28.543f, -28.152f, -27.761f, -27.37f, -26.979f, -26.588f, -26.197f, -25.806f, -25.415f, 
    -25.024f, -24.633f, -24.242f, -23.851f, -23.46f, -23.069f, -22.678f, -22.287f, -21.896f, -21.505f, -21.114f, -20.723f, -20.332f, -19.941f, -19.55f, -19.159f, 
    -18.768f, -18.377f, -17.986f, -17.595f, -17.204f, -16.813f, -16.422f, -16.031f, -15.64f, -15.249f, -14.858f, -14.467f, -14.076f, -13.685f, -13.294f, -12.903f, 
    -12.512f, -12.121f, -11.73f, -11.339f, -10.948f, -10.557f, -10.166f, -9.775f, -9.384f, -8.993f, -8.602f, -8.211f, -7.82f, -7.429f, -7.038f, -6.647f, 
    -6.256f, -5.865f, -5.474f, -5.083f, -4.692f, -4.301f, -3.91f, -3.519f, -3.128f, -2.737f, -2.346f, -1.955f, -1.564f, -1.173f, -0.782f, -0.391f, 
    0.f, 0.391f, 0.782f, 1.173f, 1.564f, 1.955f, 2.346f, 2.737f, 3.128f, 3.519f, 3.91f, 4.301f, 4.692f, 5.083f, 5.474f, 5.865f, 
    6.256f, 6.647f, 7.038f, 7.429f, 7.82f, 8.211f, 8.602f, 8.993f, 9.384f, 9.775f, 10.166f, 10.557f, 10.948f, 11.339f, 11.73f, 12.121f, 
    12.512f, 12.903f, 13.294f, 13.685f, 14.076f, 14.467f, 14.858f, 15.249f, 15.64f, 16.031f, 16.422f, 16.813f, 17.204f, 17.595f, 17.986f, 18.377f, 
    18.768f, 19.159f, 19.55f, 19.941f, 20.332f, 20.723f, 21.114f, 21.505f, 21.896f, 22.287f, 22.678f, 23.069f, 23.46f, 23.851f, 24.242f, 24.633f, 
    25.024f, 25.415f, 25.806f, 26.197f, 26.588f, 26.979f, 27.37f, 27.761f, 28.152f, 28.543f, 28.934f, 29.325f, 29.716f, 30.107f, 30.498f, 30.889f, 
    31.28f, 31.671f, 32.062f, 32.453f, 32.844f, 33.235f, 33.626f, 34.017f, 34.408f, 34.799f, 35.19f, 35.581f, 35.972f, 36.363f, 36.754f, 37.145f, 
    37.536f, 37.927f, 38.318f, 38.709f, 39.1f, 39.491f, 39.882f, 40.273f, 40.664f, 41.055f, 41.446f, 41.837f, 42.228f, 42.619f, 43.01f, 43.401f, 
    43.792f, 44.183f, 44.574f, 44.965f, 45.356f, 45.747f, 46.138f, 46.529f, 46.92f, 47.311f, 47.702f, 48.093f, 48.484f, 48.875f, 49.266f, 49.657f, 
};
static float contbl_Cr_G[256] = { 
    -104.064f, -103.251f, -102.438f, -101.625f, -100.812f, -99.999f, -99.186f, -98.373f, -97.56f, -96.747f, -95.934f, -95.121f, -94.308f, -93.495f, -92.682f, -91.869f, 
    -91.056f, -90.243f, -89.43f, -88.617f, -87.804f, -86.991f, -86.178f, -85.365f, -84.552f, -83.739f, -82.926f, -82.113f, -81.3f, -80.487f, -79.674f, -78.861f, 
    -78.048f, -77.235f, -76.422f, -75.609f, -74.796f, -73.983f, -73.17f, -72.357f, -71.544f, -70.731f, -69.918f, -69.105f, -68.292f, -67.479f, -66.666f, -65.853f, 
    -65.04f, -64.227f, -63.414f, -62.601f, -61.788f, -60.975f, -60.162f, -59.349f, -58.536f, -57.723f, -56.91f, -56.097f, -55.284f, -54.471f, -53.658f, -52.845f, 
    -52.032f, -51.219f, -50.406f, -49.593f, -48.78f, -47.967f, -47.154f, -46.341f, -45.528f, -44.715f, -43.902f, -43.089f, -42.276f, -41.463f, -40.65f, -39.837f, 
    -39.024f, -38.211f, -37.398f, -36.585f, -35.772f, -34.959f, -34.146f, -33.333f, -32.52f, -31.707f, -30.894f, -30.081f, -29.268f, -28.455f, -27.642f, -26.829f, 
    -26.016f, -25.203f, -24.39f, -23.577f, -22.764f, -21.951f, -21.138f, -20.325f, -19.512f, -18.699f, -17.886f, -17.073f, -16.26f, -15.447f, -14.634f, -13.821f, 
    -13.008f, -12.195f, -11.382f, -10.569f, -9.756f, -8.943f, -8.13f, -7.317f, -6.504f, -5.691f, -4.878f, -4.065f, -3.252f, -2.439f, -1.626f, -0.813f, 
    0.f, 0.813f, 1.626f, 2.439f, 3.252f, 4.065f, 4.878f, 5.691f, 6.504f, 7.317f, 8.13f, 8.943f, 9.756f, 10.569f, 11.382f, 12.195f, 
    13.008f, 13.821f, 14.634f, 15.447f, 16.26f, 17.073f, 17.886f, 18.699f, 19.512f, 20.325f, 21.138f, 21.951f, 22.764f, 23.577f, 24.39f, 25.203f, 
    26.016f, 26.829f, 27.642f, 28.455f, 29.268f, 30.081f, 30.894f, 31.707f, 32.52f, 33.333f, 34.146f, 34.959f, 35.772f, 36.585f, 37.398f, 38.211f, 
    39.024f, 39.837f, 40.65f, 41.463f, 42.276f, 43.089f, 43.902f, 44.715f, 45.528f, 46.341f, 47.154f, 47.967f, 48.78f, 49.593f, 50.406f, 51.219f, 
    52.032f, 52.845f, 53.658f, 54.471f, 55.284f, 56.097f, 56.91f, 57.723f, 58.536f, 59.349f, 60.162f, 60.975f, 61.788f, 62.601f, 63.414f, 64.227f, 
    65.04f, 65.853f, 66.666f, 67.479f, 68.292f, 69.105f, 69.918f, 70.731f, 71.544f, 72.357f, 73.17f, 73.983f, 74.796f, 75.609f, 76.422f, 77.235f, 
    78.048f, 78.861f, 79.674f, 80.487f, 81.3f, 82.113f, 82.926f, 83.739f, 84.552f, 85.365f, 86.178f, 86.991f, 87.804f, 88.617f, 89.43f, 90.243f, 
    91.056f, 91.869f, 92.682f, 93.495f, 94.308f, 95.121f, 95.934f, 96.747f, 97.56f, 98.373f, 99.186f, 99.999f, 100.812f, 101.625f, 102.438f, 103.251f, 
};
static float contbl_Cr_R[256] = { 
    -204.288f, -202.692f, -201.096f, -199.5f, -197.904f, -196.308f, -194.712f, -193.116f, -191.52f, -189.924f, -188.328f, -186.732f, -185.136f, -183.54f, -181.944f, -180.348f, 
    -178.752f, -177.156f, -175.56f, -173.964f, -172.368f, -170.772f, -169.176f, -167.58f, -165.984f, -164.388f, -162.792f, -161.196f, -159.6f, -158.004f, -156.408f, -154.812f, 
    -153.216f, -151.62f, -150.024f, -148.428f, -146.832f, -145.236f, -143.64f, -142.044f, -140.448f, -138.852f, -137.256f, -135.66f, -134.064f, -132.468f, -130.872f, -129.276f, 
    -127.68f, -126.084f, -124.488f, -122.892f, -121.296f, -119.7f, -118.104f, -116.508f, -114.912f, -113.316f, -111.72f, -110.124f, -108.528f, -106.932f, -105.336f, -103.74f, 
    -102.144f, -100.548f, -98.952f, -97.356f, -95.76f, -94.164f, -92.568f, -90.972f, -89.376f, -87.78f, -86.184f, -84.588f, -82.992f, -81.396f, -79.8f, -78.204f, 
    -76.608f, -75.012f, -73.416f, -71.82f, -70.224f, -68.628f, -67.032f, -65.436f, -63.84f, -62.244f, -60.648f, -59.052f, -57.456f, -55.86f, -54.264f, -52.668f, 
    -51.072f, -49.476f, -47.88f, -46.284f, -44.688f, -43.092f, -41.496f, -39.9f, -38.304f, -36.708f, -35.112f, -33.516f, -31.92f, -30.324f, -28.728f, -27.132f, 
    -25.536f, -23.94f, -22.344f, -20.748f, -19.152f, -17.556f, -15.96f, -14.364f, -12.768f, -11.172f, -9.576f, -7.98f, -6.384f, -4.788f, -3.192f, -1.596f, 
    0.f, 1.596f, 3.192f, 4.788f, 6.384f, 7.98f, 9.576f, 11.172f, 12.768f, 14.364f, 15.96f, 17.556f, 19.152f, 20.748f, 22.344f, 23.94f, 
    25.536f, 27.132f, 28.728f, 30.324f, 31.92f, 33.516f, 35.112f, 36.708f, 38.304f, 39.9f, 41.496f, 43.092f, 44.688f, 46.284f, 47.88f, 49.476f, 
    51.072f, 52.668f, 54.264f, 55.86f, 57.456f, 59.052f, 60.648f, 62.244f, 63.84f, 65.436f, 67.032f, 68.628f, 70.224f, 71.82f, 73.416f, 75.012f, 
    76.608f, 78.204f, 79.8f, 81.396f, 82.992f, 84.588f, 86.184f, 87.78f, 89.376f, 90.972f, 92.568f, 94.164f, 95.76f, 97.356f, 98.952f, 100.548f, 
    102.144f, 103.74f, 105.336f, 106.932f, 108.528f, 110.124f, 111.72f, 113.316f, 114.912f, 116.508f, 118.104f, 119.7f, 121.296f, 122.892f, 124.488f, 126.084f, 
    127.68f, 129.276f, 130.872f, 132.468f, 134.064f, 135.66f, 137.256f, 138.852f, 140.448f, 142.044f, 143.64f, 145.236f, 146.832f, 148.428f, 150.024f, 151.62f, 
    153.216f, 154.812f, 156.408f, 158.004f, 159.6f, 161.196f, 162.792f, 164.388f, 165.984f, 167.58f, 169.176f, 170.772f, 172.368f, 173.964f, 175.56f, 177.156f, 
    178.752f, 180.348f, 181.944f, 183.54f, 185.136f, 186.732f, 188.328f, 189.924f, 191.52f, 193.116f, 194.712f, 196.308f, 197.904f, 199.5f, 201.096f, 202.692f, 
};


static CMRGB*
convertYUV400toCMRGB(
    unsigned char*  yuv,
    bool            /* isPlanar */,
    int             width,
    int             height
    )
{
    CMRGB* rgb = new CMRGB[width*height];
    unsigned char** pYUV = (unsigned char**)yuv;
    unsigned char val;
    int x, y, i;

    i = 0;
    for (y=0; y<height; y++) {
        for (x=0; x<width; x++) {
            val = pYUV[y][x];
            rgb[i].r = val;
            rgb[i].g = val;
            rgb[i].b = val;
            i++;
        }
    }

    return rgb;
}

/*
 * YUV420 to CMRGB Conversion equation:
 * B = 1.164(Y - 16)                   + 2.018(U - 128)
 * G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
 * R = 1.164(Y - 16) + 1.596(V - 128)
 */
static CMRGB*
convertYUV420toCMRGB(
    unsigned char*  yuv,
    bool            isPlanar,
    int             width,
    int             height
    )
{
    CMRGB*           rgb = new CMRGB[width*height];
    unsigned char* yy = yuv;
    unsigned char* cb  = yuv + width*height;
    unsigned char* cr  = cb + width*height/4;
    int            i, x, y, incY, incUV;
    int            Y, Cb, Cr, R, G, B;
    LARGE_INTEGER  frequency, s_counter, e_counter, diff;

    QueryPerformanceFrequency(&frequency);

    QueryPerformanceCounter(&s_counter);
    i = incY = incUV = 0;
    for (y=0; y<height; y++) {
        for (x=0; x<width; x++) {
            Y  = yy[incY++];
            incUV = y/2*width/2 + x/2;
            Cb = cb[incUV];
            Cr = cr[incUV];

            B = (int)(contbl_Y[Y] + contbl_Cb_B[Cb]);
            G = (int)(contbl_Y[Y] - contbl_Cr_G[Cr] - contbl_Cb_G[Cb]);
            R = (int)(contbl_Y[Y] + contbl_Cr_G[Cr]);

            rgb[i].r = CLIPPING(R);
            rgb[i].g = CLIPPING(G);
            rgb[i].b = CLIPPING(B);
            i++;
        }
    }
    QueryPerformanceCounter(&e_counter);

    diff.QuadPart = e_counter.QuadPart - s_counter.QuadPart;
    double elaspedus = (double)(diff.QuadPart*1000000 / frequency.QuadPart);

    return rgb;
}

static CMRGB*
convertYUV422toCMRGB(
    unsigned char*  yuv,
    bool            isPlanar,
    int             width,
    int             height
    )
{
    CMRGB*           rgb = new CMRGB[width*height];
    unsigned char* yy = yuv;
    unsigned char* cb = yy + width*height;
    unsigned char* cr = cb + width*height/2;
    int            i, x, y, incY, incUV;
    int            Y, Cb, Cr, R, G, B;

    i = incY = incUV = 0;
    for (y=0; y<height; y++) {
        for (x=0; x<width; x++) {
            Y  = yy[incY++];
            incUV = y*width/2 + x/2;
            Cb = cb[incUV];
            Cr = cr[incUV];
            B = (int)(contbl_Y[Y] + contbl_Cb_B[Cb]);
            G = (int)(contbl_Y[Y] - contbl_Cr_G[Cr] - contbl_Cb_G[Cb]);
            R = (int)(contbl_Y[Y] + contbl_Cr_G[Cr]);
            rgb[i].r = CLIPPING(R);
            rgb[i].g = CLIPPING(G);
            rgb[i].b = CLIPPING(B);
            i++;
        }
    }

    return rgb;
}

static CMRGB*
convertYUV444toCMRGB(
    unsigned char*  yuv,
    bool            isPlanar,
    int             width,
    int             height
    )
{
    CMRGB*           rgb = new CMRGB[width*height];
    unsigned char* yy  = yuv;
    unsigned char* cb  = yy + width*height;
    unsigned char* cr  = cb + width*height;
    int            i, x, y, incY, incUV;
    int            Y, Cb, Cr, R, G, B;

    i = incY = incUV = 0;
    for (y=0; y<height; y++) {
        for (x=0; x<width; x++) {
            Y  = yy[incY++];
            Cb = cb[incUV];
            Cr = cr[incUV++];
            B = (int)(contbl_Y[Y] + contbl_Cb_B[Cb]);
            G = (int)(contbl_Y[Y] - contbl_Cr_G[Cr] - contbl_Cb_G[Cb]);
            R = (int)(contbl_Y[Y] + contbl_Cr_G[Cr]);
            rgb[i].r = CLIPPING(R);
            rgb[i].g = CLIPPING(G);
            rgb[i].b = CLIPPING(B);
            i++;
        }
    }

    return rgb;
}


CMRGB*
CMImage::convertYUVtoRGB(
    unsigned char*  yuv, 
    int             width,
    int             height,
    CMYUVFormat     format
    )
{
    CMRGB* rgb = NULL;
    bool isPlanar = (format == CM_IMG_FMT_YUV_PLANAR);

    switch (format) {
    case CM_YUV_FMT_400: rgb = convertYUV400toCMRGB(yuv, isPlanar, width, height); break;
    case CM_YUV_FMT_420: rgb = convertYUV420toCMRGB(yuv, isPlanar, width, height); break;
    case CM_YUV_FMT_422: rgb = convertYUV422toCMRGB(yuv, isPlanar, width, height); break;
    case CM_YUV_FMT_444: rgb = convertYUV444toCMRGB(yuv, isPlanar, width, height); break;
    }

    return rgb;
}